/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import java.util.List;
import java.util.Set;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import static kimo.bl.zkUtils.setDateboxValue;
import static kimo.bl.zkUtils.setTextboxValue;
import kimo.persistence.controllers.AdtTypeJpaController;
import kimo.persistence.controllers.SexJpaController;
import kimo.persistence.entities.AdtType;
import kimo.persistence.entities.Producer;
import kimo.persistence.entities.Sex;
import labels.LabelsEnum;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author a381u296
 */
public class ProdPersonal extends SelectorComposer<Component> {

    @Wire
    private Textbox producer_afm;
    @Wire
    private Textbox producer_surname;
    @Wire
    private Textbox producer_first_name;
    @Wire
    private Textbox producer_father_name;
    @Wire
    private Datebox producer_birthdate;
    @Wire
    private Listbox producer_sex_id;
    @Wire
    private Listbox producer_adtType_id;
    @Wire
    private Textbox producer_adt;
    @Wire
    private Datebox producer_adt_issue_date;
    @Wire
    private Textbox producer_adt_issue_department;
    @Wire
    private Textbox producer_phone;
    @Wire
    private Textbox producer_mobile_phone;
    @Wire
    private Textbox producer_email;

    private SexJpaController sexJpaController;
    private List<Sex> lstSex;
    private ListModelList<Sex> lmlSex;

    private AdtTypeJpaController adtTypeJpaController;
    private List<AdtType> lstAdtTypes;
    private ListModelList<AdtType> lmlAdtTypes;

    private Producer producer;

    private KimoDAO pdao;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        populateListboxs();
        pdao = new KimoDAO();
        producer = pdao.findById(Executions.getCurrent().getParameter("pid"));
        if (producer != null) {
            updateUI(producer);
        }
    }

    public void populateListboxs() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("KimoPU");

        sexJpaController = new SexJpaController(emf);
        lstSex = sexJpaController.findSexEntities();
        lmlSex = new ListModelList<Sex>(lstSex);
        producer_sex_id.setModel(lmlSex);

        adtTypeJpaController = new AdtTypeJpaController(emf);
        lstAdtTypes = adtTypeJpaController.findAdtTypeEntities();
        lmlAdtTypes = new ListModelList<AdtType>(lstAdtTypes);
        producer_adtType_id.setModel(lmlAdtTypes);

    }

    public void updateUI(Producer p) {
        setTextboxValue(producer_afm, p.getProducerAfm());
        setTextboxValue(producer_surname, p.getProducerSurname());
        setTextboxValue(producer_first_name, p.getProducerFirstName());
        setTextboxValue(producer_father_name, p.getProducerFatherName());
        setDateboxValue(producer_birthdate, p.getProducerBirthdate());
        ((ListModelList) producer_sex_id.getModel())
                .addToSelection(p.getProducerSexId());
        ((ListModelList) producer_adtType_id.getModel())
                .addToSelection(p.getProducerAdtTypeId());

        setTextboxValue(producer_adt, p.getProducerAdt());
        setDateboxValue(producer_adt_issue_date, p.getProducerAdtIssueDate());
        setTextboxValue(producer_adt_issue_department,
                p.getProducerAdtIssueDepartment());
        setTextboxValue(producer_phone, p.getProducerPhone());
        setTextboxValue(producer_mobile_phone, p.getProducerMobilePhone());
        setTextboxValue(producer_email, p.getProducerEmail());
    }

    public void getFromUI(Producer p) {
        p.setProducerSurname(producer_surname.getText());
        p.setProducerFirstName(producer_first_name.getText());
        p.setProducerFatherName(producer_father_name.getText());
        p.setProducerBirthdate(producer_birthdate.getValue());
        Set<Sex> sex = ((ListModelList) producer_sex_id.getModel()).getSelection();
// δεν ξερω για ποιο λογο ακριβως αλλα τα listbox δεν εχουν constraints
// αρα πρεπει να το κανεις manual !!
// δηλαδη αμα δεν εχει τιμη πεταω ενα wrong value exception
        if (!sex.isEmpty()) { // εδω τσεκαρει να δει αν υπαρχει επιλογη
            Sex pSex = sex.iterator().next();
            p.setProducerSexId(pSex);
        } else {
            throw new WrongValueException(producer_sex_id,
                    Labels.getLabel(LabelsEnum.SELECT_SEX.toString()));
        }

        Set<AdtType> adtTypes = ((ListModelList) producer_adtType_id.getModel()).getSelection();
        if (!adtTypes.isEmpty()) {
            AdtType adtType = adtTypes.iterator().next();
            p.setProducerAdtTypeId(adtType);
        } else {
            throw new WrongValueException(producer_adtType_id,
                    Labels.getLabel(LabelsEnum.SELECT_ADT_TYPE.toString()));

        }
        p.setProducerAdt(producer_adt.getText());
        p.setProducerAdtIssueDate(producer_adt_issue_date.getValue());
        p.setProducerAdtIssueDepartment(producer_adt_issue_department.getValue());
        p.setProducerPhone(producer_phone.getValue().toString());
        p.setProducerMobilePhone(producer_mobile_phone.getRawText());
        p.setProducerEmail(producer_email.getValue());

    }

    @Listen("onClick = #saveProducerBtn")
    public void onSaveProducerBtnClick() throws Exception {
       // Messagebox.show("H Αποθήκευση αφορα το σύνολο των στοιχείων !") ;

        getFromUI(producer);
        if (pdao.save((producer)).equals("ok")) {
            Messagebox.show("Τα στοιχεία του παραγωγού ενημερώθηκαν");

        }
    }

}
